<?php

use Utils\Funciones;
use Utils\Usuarios;
use Lib\Dao\UsuariosDao;
use Lib\Dao\LogsDao;
use Lib\Utils\UsuariosUtils;
use Lib\Utils\MenusUtils;
use Lib\Dao\MenusDao;
 

class TestController extends \BaseController {

	var $usuariosDao;
	var $usuariosUtils;

	
	public function __construct(){
		$this->usuariosDao = new UsuariosDao();
		$this->usuariosUtils = new UsuariosUtils();
	}

	public function index(){
		$this->layout = '';
		$usuariosDao = new UsuariosDao();
		$bbdd = $usuariosDao->infoMysql();
		
		$aux = array();
		foreach($bbdd as $row){
			$new = new \stdClass();
			$new->bbdd = $row->table_schema;
			$new->tablas = $row->TABLES;
			$new->rows = $row->ROWS;
			$new->data = $row->DATA;
			$aux['data'][] = $new;			
		}
		echo json_encode($aux);
		
		/*
		
		DB::transaction(function(){
				$total = 0;
				for($i=0;$i<=10000;$i++){
						LogsDao::nuevoBug("TEST");
					$total++;
					echo($total."<br>");
				}
					
			});
		
		
		*/
}
	
	public function pruebas(){
		$this->layout = '';
		$menus = new MenusDao();
		$result = MenusUtils::ordenarMenusConSubmenus($menus->getMenusPerIdGestor(1));
		$result = MenusUtils::montarListadoNestableMenus($result);
		$url="http://www.respecte.cat/admin/login/inicio_sesion";
		$agent= 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';
		
		$curl_header = array('X-Requested-With: XMLHttpRequest');
		
		$ch = curl_init();
		
		for($i=0;$i<=1000000;$i++){
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_VERBOSE, true);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_HTTPHEADER,$curl_header);
			curl_setopt($ch, CURLOPT_REFERER, 'http://www.respecte.cat/admin/login');
			curl_setopt($ch, CURLOPT_POSTFIELDS,"u=mario&p=corredera");
			curl_setopt($ch, CURLOPT_USERAGENT, $agent);
			curl_setopt($ch, CURLOPT_URL,$url);
			$result=curl_exec($ch);
			var_dump($result);
		}
		print_r($result);
	}	

	
	public function bucleUsuarios(){
		$this->layout = '';
	
		$usuariosDao = new UsuariosDao();
	
		$strings = array(true,false);
	
		for($i=0;$i<20;$i++){
			$usuario['nombre'] = "Diego Armando Maradona";
			$usuario['usuario'] = "test".$i;
			$usuario['email'] = "mario".$i."@fcf.com";
			$usuario['password'] = Hash::make("corredera");
			$usuario['menu_defecto'] = '{"id":"3","permiso":"2","vista":"internacional"}';
			$usuario['menu_sup'] = '[{"id":"2","nombre":"Formaciu00f3","permiso":"3","color":"tiles-brown","icono":"fa-cog"}]';
			$usuario['rol'] = 3;
			$usuario['activo'] = array_rand($strings);
			$result = $usuariosDao->crearUsuario($usuario);
		}
	
		echo json_encode($result);
	}
	
	
	public function animacion(){
		$this->getMenuUsuario();
		$this->layout->content = View::make('test.animacion');
	}
	
	
	public function artisan(){
		$this->layout = '';
		/*
		$php = "D:\\xampp\php\php.exe ";
		$artisan = "D:\\xampp\htdocs\admin_fcf ";
		try {
			//exec($php.'artisan clear-compiled',$output, $return_var);
			exec($php.'artisan migrate',$output, $return_var);
			echo("OK<br><br><br><br>");
	
			foreach($output as $row){
				print_r($row);echo("<br>");
			}
		
		}catch(Exception $e) {
			print_r($e->getMessage());
		}
		
		/*
		Schema::table('usuarios', function($table){
			$table->boolean('activo');
		}); */
		

	}
	
	function correo(){
		$this->layout = '';
		
		$data['test'] = "mario";
		
		/*
		try{
			$result = Mail::send('emails.welcome',$data, function($message){
		   		$message->to('mario@fcf.cat', 'John Smith')->subject('Welcome!');
			});
			print_r($result);
		}catch(Exception $e){
			print_r($e->getMessage());
		}*/
		
		
		/*
		try{
			$result = mail("mario@fcf.cat","mario@fcf.cat","hola");
			print_r($result);
		}catch(Exception $e){
			print_r($e->getMessage());
		}
		*/
		
	
	}
	
	function falta($hash){
		$this->getMenuUsuario();
		$this->layout->content = View::make('test.falta',array('info' =>'ddd')); 
		//echo("OPTIMIZAR QUERY : SELECT id,id_gestor,nombre,permiso,enlace,parent,icono,gestor FROM getmenuusuario WHERE id_gestor=1 AND permiso>=2");
	}
	
	function __destruct(){
		$this->usuariosDao;
	}


}


?>